package com.yuanfengerp.basicInfoManage.svipinfo.service.impl;

import com.yuanfengerp.basicInfoManage.svipinfo.dao.IMemberinfoDao;
import com.yuanfengerp.basicInfoManage.svipinfo.model.Memberinfo;
import com.yuanfengerp.basicInfoManage.svipinfo.service.IMemberinfoService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MenmberinfoImpl implements IMemberinfoService {
    @Autowired
    private IMemberinfoDao imbi;
    @Override
    public Result<List<Memberinfo>> selectMemberAll(Map<String, Object> params) {
        params.put("pagenum",((int) params.get("pagenum")-1) * (int) params.get("num"));
        List<Memberinfo> memberinfos = imbi.selectMemberAll(params);
        if (!(memberinfos.isEmpty()))
            return Result.success(memberinfos, imbi.Count());
        return Result.failure(400,"没有这条信息");
    }

    @Override
    public Result<String> addMember(Memberinfo memberinfo) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(001);
        memberinfo.setMember_id(idWorker.nextId());
        memberinfo.setMember_time(new java.sql.Date(System.currentTimeMillis()));
        memberinfo.setMember_amendtime(new java.sql.Date(System.currentTimeMillis()));
        memberinfo.setMember_state(1);

        int i = imbi.addMember(memberinfo);
        if (i > 0)
            return Result.success("添加成功");
        return Result.failure(400,"添加失败");
    }

    @Override
    public Result<String> updateMember(Memberinfo memberinfo) {
//        System.out.println(memberinfo.getMember_name());
        Integer integer = imbi.updateMember(memberinfo);
        if (integer > 0)
            return Result.success("修改成功");
        return Result.failure(400,"修改失败");
    }

    @Override
    public Result<Memberinfo> selectMember(String id) {
        Memberinfo memberinfo = imbi.selectMember(id);
        if (memberinfo != null)
            return Result.success(memberinfo);
        return Result.failure(400,"没找到");
    }
}
